﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Besture.Bll;
using Newtonsoft.Json;
using Besture.Model;
using Besture.Common.Definition;
using Besture.Utils;

namespace Besture.UI.Ajax
{
    public partial class AjaxPost : System.Web.UI.Page
    {
        /// <summary>
        /// Ajax请求的方法名
        /// </summary>
        private string MethodName
        {
            get { return Request.Params["action"]; }
        }
        /// <summary>
        /// Ajax请求的参数
        /// </summary>
        private string JsonString
        {
            get { return Request.Params["JsonString"]; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            MethodInfo mInfo = this.GetType().GetMethod(MethodName);
            if (mInfo == null)
            {
                throw new Exception("找不到方法：" + MethodName);
            }
            object[] param = new object[1];
            if (mInfo.GetParameters().Length > 0)
            {
                param[0] = JsonConvert.DeserializeObject<Dictionary<string, object>>(JsonString);
            }
            object result = mInfo.Invoke(this, param);
            if (result != null)
            {
                Response.ContentType = "text/plain";
                Response.Charset = "utf-8";
                Response.Write(JsonConvert.SerializeObject(result));
                Response.End();
            }
        }

        /// <summary>
        /// 根据城市Id 获取工作机会
        /// </summary>
        /// <param name="paramsDic"></param>
        /// <returns></returns>
        public string GetJobChance(Dictionary<string, object> paramsDic)
        {
            StringBuilder html = new StringBuilder();
            DataTable dt = new JobChanceDac().GetDataTableByCityId(Convert.ToInt32(paramsDic["CityId"]));
            if (dt != null)
            {
                string tempHtml = "<p><i>{0}.</i> <span>{1}</span></p>";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    html.Append(string.Format(tempHtml, i + 1, dt.Rows[i]["PositionTitle"]));
                }
            }
            return html.ToString().Trim();
        }


        /// <summary>
        /// 报名
        /// </summary>
        /// <param name="paramsDic"></param>
        /// <returns></returns>
        public string SubmitActive(Dictionary<string, object> paramsDic)
        {
            string msg = "报名成功";
            object user = Session[Constants.SESSION_KEY_LOGIN_USER];
            if (user != null)
            {
                UserActiveModel model = new UserActiveModel();
                UserActiveDac bll = new UserActiveDac();
                model.ANID = Convert.ToInt32(paramsDic["ID"]);
                model.UID = (user as UserModel).ID;
                model.IsDeleted = 0;
                model.JoinDate = DateTime.Now;
                model.CreateTime = DateTime.Now;

                msg = bll.SubmitActive(model,(user as UserModel).Phone);
            }
            else
            {
                msg = "请先登录后再报名";
            }
            return msg;
        }
    }
}